<?php

class Application_Model_Details {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Application_Model_DbTable_Details();
    }

    /**
     * 
     * @param type $projectId
     * @param type $type
     * @return array
     */
    public function fetchAll($projectId, $type) {
        $select = $this->_dbtable->select();
        $select->where("project_id = ? ", $projectId);
        $select->where("type = ? ", $type);
        $fetchAll = $this->_dbtable->fetchAll($select);
        $array = array();
        foreach ($fetchAll as $data) {
            array_push($array, $this->formatData($data));
        }
        return $array;
    }

    /**
     * 
     * @param type $id
     * @return type
     */
    public function getById($id) {
        $select = $this->_dbtable->select();
        $select->where("id = ? ", $id);
        $fetchAll = $this->_dbtable->fetchAll($select);
        return $this->formatData($fetchAll[0]);
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    private function formatData($data) {
        $globalArray = json_decode($data['details']);
        $optionsDb = new Application_Model_Options();
        $detailType = $optionsDb->getByName("detailType");
        $featureStatus = $optionsDb->getByName("featureStatus");
        $class = "";
        switch ($data['status']) {
            case 1:
                $class = "alert alert-info";
                break;
            case 2:
                $class = "alert alert-success";
                break;
            case 3:
            case 4:
                $class = "alert alert-danger";
                break;
            default:
                break;
        }
        $myArray = array(
            'id' => $data['id'],
            'project_id' => $data['project_id'],
            'details' => array(
                'main' => $globalArray->main,
                'comments' => $globalArray->comments,
            ),
            'type' => $detailType['details'][$data['type']],
            'status' => $featureStatus['details'][$data['status']],
            'status_id' => $data['status'],
            'class' => $class,
        );
        return $myArray;
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    public function createNew($data) {
        return $this->_dbtable->insert($data);
    }

    /**
     * 
     * @param type $data
     * @param type $id
     * @return type
     */
    public function updateData($data, $id) {
        return $this->_dbtable->update($data, "id = $id");
    }

}

